home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / gcc / libnix.lha / gnu / lib / libnix / sources.lha / detach / __initdetach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-20  |  2.6 KB  |  96 lines

  1. #include <exec/memory.h>
  2. #include <dos/dosextens.h>
  3. #include <dos/dostags.h>
  4. #ifdef __GNUC__
  5. #include <inline/exec.h>
  6. #include <inline/dos.h>
  7. #endif
  8. #include <stabs.h>
  9.  
  10. extern struct WBStart *_WBenchMsg;
  11. extern char *__commandline;
  12. extern struct DosLibrary *DOSBase;
  13. extern struct ExecBase *SysBase;
  14. extern void *__SaveSP;
  15. extern char __dosname[];
  16.  
  17. extern long __stack;
  18. extern char *__procname;
  19. extern long __priority;
  20.  
  21. /* I must close a library after my child is running -
  22.  * and closing a library requires a working dispatcher, so:
  23.  */
  24. static struct SignalSemaphore *sem;
  25.  
  26. void __initdetach(void)
  27.   if(_WBenchMsg!=NULL)
  28.     return;
  29.  
  30.   if(sem!=NULL)           /* I must be the child process */
  31.   { ObtainSemaphore(sem); /* Assert that my parent is already dead */
  32.     ReleaseSemaphore(sem);
  33.     FreeMem(sem,sizeof(struct SignalSemaphore));
  34.     return;
  35.   }
  36.                           /* I must be the parent */
  37.   if((sem=(struct SignalSemaphore *)
  38.          AllocMem(sizeof(struct SignalSemaphore),MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  39.     exit(20);
  40.  
  41.   InitSemaphore(sem);
  42.     
  43.   ObtainSemaphore(sem); /* Assert that my child is suspended until I'm finished */
  44.     
  45.   if((DOSBase=(struct DosLibrary *)OpenLibrary(__dosname,37))!=NULL)
  46.   { struct CommandLineInterface *cli;
  47.     void *stack;
  48.  
  49.     cli=Cli();
  50.     { struct TagItem tags[7];
  51.       
  52.       tags[0].ti_Tag =NP_Seglist;     /* child process gets my seglist */
  53.       tags[0].ti_Data=cli->cli_Module;
  54.  
  55.       tags[1].ti_Tag =NP_FreeSeglist; /* and must free it */
  56.       tags[1].ti_Data=1;
  57.  
  58.       tags[2].ti_Tag =NP_StackSize;   /* it gets a stack */
  59.       tags[2].ti_Data=__stack;
  60.  
  61.       tags[3].ti_Tag =NP_Name;        /* a name */
  62.       tags[3].ti_Data=(ULONG)__procname;
  63.  
  64.       tags[4].ti_Tag =NP_Priority;    /* a priority */
  65.       tags[4].ti_Data=__priority;
  66.  
  67.       tags[5].ti_Tag =NP_Arguments;   /* and my commandline Arguments */
  68.       tags[5].ti_Data=(ULONG)__commandline;
  69.  
  70.       tags[6].ti_Tag =TAG_END;        /* That's all - everything else is implicit */
  71.  
  72.       stack=__SaveSP;
  73.  
  74.       if(CreateNewProc(tags)!=NULL)
  75.       { cli->cli_Module=0;            /* I'm no longer owner of this */
  76.         CloseLibrary((struct Library *)DOSBase);
  77.         DOSBase=NULL;
  78.  
  79.         Forbid(); /* Disable Multitasking */
  80.         ReleaseSemaphore(sem);
  81.  
  82.         /* Adjust stack, enable multitasking, return 0 */
  83.         asm("movel %0,sp;movel %1,a6;moveql #0,d0;jmp a6@(-138)"::
  84.             "r"(stack),"r"(SysBase):"a6");
  85.       }
  86.       CloseLibrary((struct Library *)DOSBase);
  87.     }
  88.   }
  89.   ReleaseSemaphore(sem);
  90.  
  91.   exit(20);
  92. }
  93.   
  94. ADD2INIT(__initdetach,-70); /* A very high priority */
  95.